/**
 * Created by ${PRODUCT_NAME}.
 * User: skylerparr
 * Date: 2/6/11
 * Time: 10:38 AM
 * To change this template use File | Settings | File Templates.
 */
package com.thoughtorigin.display.containers {
import flash.display.DisplayObject;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;

public class DefaultScroller extends AbstractScroller {

    private static const SCROLL_WIDTH: Number = 5;

    private var _trackSkin: Sprite;
    private var _thumbSkin: Sprite;
    private var _dragArea: Sprite;
    private var _scrollLength: Number = 100;
    private var _stage: Stage;

    public function DefaultScroller() {
        buildSkin();
    }

    protected function buildSkin(): void {
        _trackSkin = new Sprite();
        _thumbSkin = new Sprite();
        drawShape(_trackSkin, 0xAAAAAA, SCROLL_WIDTH, _scrollLength);
        drawShape(_thumbSkin, 0x555555, SCROLL_WIDTH, SCROLL_WIDTH);

        addChild(_trackSkin);
        addChild(_thumbSkin);

        _dragArea = new Sprite();
        drawShape(_dragArea, 0, _trackSkin.width - _thumbSkin.width, _trackSkin.height - _thumbSkin.height);

        _thumbSkin.buttonMode = true;

        addEventListener(Event.ADDED_TO_STAGE, function(e: Event): void {
            removeEventListener(Event.ADDED_TO_STAGE, arguments.callee);

            _stage = _thumbSkin.stage;

            _thumbSkin.addEventListener(MouseEvent.MOUSE_DOWN, onThumbSkinMouseDown, false, 0, true);
            _stage.addEventListener(MouseEvent.MOUSE_UP, onThumbSkinMouseUp, false, 0, true);
        });
    }

    override public function useHorizontalScroll(value:Boolean):void {
        if(value) {
            rotation = 270;
        } else {
            rotation = 0;
        }
    }

    override public function set scrollLength(value : Number): void {
        _scrollLength = value;
        drawShape(_trackSkin, 0xAAAAAA, SCROLL_WIDTH, _scrollLength);
        drawShape(_dragArea, 0, _trackSkin.width - _thumbSkin.width, _trackSkin.height - _thumbSkin.height);
    }

    private function onThumbSkinMouseUp(event:MouseEvent):void {
        _thumbSkin.stopDrag();
        _stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    }

    private function onThumbSkinMouseDown(event:MouseEvent):void {
        _thumbSkin.startDrag(true, _dragArea.getRect(_dragArea));
        _stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true);
    }

    private function onMouseMove(event:MouseEvent):void {
        dispatchEvent(new ScrollEvent(ScrollEvent.SCROLL_UPDATE, this));
    }

    override public function get percent():Number {
        return _thumbSkin.y / _dragArea.height;
    }

    private function drawShape(control: Sprite, color: uint, width: Number, height: Number): void {
        with(control.graphics) {
            clear();
            beginFill(color);
            drawRect(0,0,width,height);
            endFill();
        }
    }

    protected function set trackSkin(value: Sprite): void {
        _trackSkin = value;
    }

    protected function set thumbSkin(value: Sprite): void {
        _thumbSkin = value;
    }

}
}
